package com.hongkzh.www.shop.service;

import com.hongkzh.www.adv.service.BaseService;
import com.hongkzh.www.conmon.BusinessException;
import com.hongkzh.www.conmon.DateUtil;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.user.service.UserService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Enhancer;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单服务
 *
 * @author martins
 */
public class OrderService extends BaseService {
    public List<Record> getUserUnpaidList(String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("order.getUserUnpaidList", loginUid);

        List<Record> list = Db.find(sqlPara);
        for (Record r : list
        ) {
            doImgPath(r);
        }
        return list;

    }

    public List<Record> getUserPaidList(String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("order.getUserPaidList", loginUid);

        List<Record> list = Db.find(sqlPara);
        for (Record r : list
        ) {
            doImgPath(r);
        }
        return list;
    }

    public List<Record> getUserUnreceivedList(String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("order.getUserUnreceivedList", loginUid);

        List<Record> list = Db.find(sqlPara);
        for (Record r : list
        ) {
            doImgPath(r);
        }
        return list;
    }

    public List<Record> getUserReceivedList(String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("order.getUserReceivedList", loginUid);

        List<Record> list = Db.find(sqlPara);
        for (Record r : list
        ) {
            doImgPath(r);
        }
        return list;
    }

    public Record getPayByOrderIds(String loginUid, String[] orderIds) {
        SqlPara sqlPara = Db.getSqlPara("user.selectUserIntegral", loginUid);
        Record record = Db.findFirst(sqlPara);
        sqlPara = Db.getSqlPara("order.getPayByOrderIds", Kv.by("orderIds", orderIds));
        int orderIntegral = 0;
        List<Record> list = Db.find(sqlPara);
        for (Record r : list
        ) {
            if (StrKit.isBlank(r.get("activityType"))) {
                orderIntegral += r.getInt("integral");
            } else {
                orderIntegral += r.getInt("activityPrice");
            }
            doImgPath(r);
        }
        record.set("orderIntegral", orderIntegral);
        record.set("products", list);
        return record;


    }

    @Before(Tx.class)
    public void buyCoupon(String couponId, String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("user.selectUserIntegral", loginUid);
        Record record = Db.findFirst(sqlPara);
        int integral = record.getInt("integral");
        sqlPara = Db.getSqlPara("shop.getCouponByCouponId", couponId);
        Record coupon = Db.findFirst(sqlPara);
        if (null == coupon) {
            throw new BusinessException("此券已卖光");
        }
        int couponIntegral = coupon.getInt("integral");
        if (integral < couponIntegral) {
            throw new BusinessException("乐币不够");
        }
        // 减少乐币
        sqlPara = Db.getSqlPara("user.reduceIntegral", couponIntegral, loginUid);
        Db.update(sqlPara);

        //增加优惠券
        Db.update(Db.getSqlPara("shop.updateFromCouponId", couponId, loginUid, IdGen.uuid()));

        //修改优惠券状态
        Db.update(Db.getSqlPara("shop.updateCouponByCouponId", couponId));
        // 增加乐币
        sqlPara = Db.getSqlPara("user.updateUserIntegral", couponIntegral, loginUid);
        Db.update(sqlPara);

    }

    @Before(Tx.class)
    public boolean payByOrderIds(String loginUid, String[] orderIds) {

        boolean flag = false;
        SqlPara sqlPara = Db.getSqlPara("user.selectUserIntegral", loginUid);
        Record record = Db.findFirst(sqlPara);
        int integral = record.getInt("integral");
        sqlPara = Db.getSqlPara("order.getPayByOrderIds", Kv.by("orderIds", orderIds));
        int orderIntegral = 0;
        List<Record> list = Db.find(sqlPara);
        for (Record r : list
        ) {
            if (StrKit.isBlank(r.get("activityType"))) {
                orderIntegral += r.getInt("integral");
            } else {
                orderIntegral += r.getInt("activityPrice");
            }
        }
        if (null != list && list.size() > 0) {
            //更改订单状态
            if (integral > orderIntegral) {
                sqlPara = Db.getSqlPara("order.updatePayByOrderIds", Kv.by("orderIds", orderIds));
                Db.update(sqlPara);
                //更改用户乐币
                sqlPara = Db.getSqlPara("user.reduceIntegral", orderIntegral, loginUid);
                Db.update(sqlPara);
                flag = true;
            }
        }

        return flag;
    }

    @Before(Tx.class)
    public void buyNewUserVipCoupon(String vipCouponId, String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("user.selectUserIntegral", loginUid);
        Record record = Db.findFirst(sqlPara);
        int integral = record.getInt("integral");

        sqlPara = Db.getSqlPara("user.myValidQualificationCoupon", loginUid);
        Record vacoupon = Db.findFirst(sqlPara);
        if (null == vacoupon) {
            throw new BusinessException("没有新人专享资格券");
        }

        sqlPara = Db.getSqlPara("shop.getNewUserVipByCouponId", vipCouponId);
        Record coupon = Db.findFirst(sqlPara);
        if (null == coupon) {
            throw new BusinessException("此券已卖光");
        }
        String couponId = coupon.getStr("couponId");
        int couponIntegral = coupon.getInt("integral");
        if (integral < couponIntegral) {
            throw new BusinessException("乐币不够");
        }
        // 减少乐币
        sqlPara = Db.getSqlPara("user.reduceIntegral", couponIntegral, loginUid);
        int i = Db.update(sqlPara);
        if (i == 0) {
            throw new BusinessException("乐币不够");
        }

        //修改优惠券状态
        i = Db.update(Db.getSqlPara("shop.updateNewUserVipCouponByCouponId", vipCouponId));
        if (i == 0) {
            throw new BusinessException("优惠券不够");
        }
        Db.update(Db.getSqlPara("user.updateValidQualificationCoupon", loginUid));


        //增加优惠券
        Db.update(Db.getSqlPara("shop.updateFromCouponId",
                IdGen.uuid(), coupon.getStr("productId"),
                coupon.getStr("couponId"), coupon.getStr("title"), coupon.getStr("integral"), loginUid)
        );

    }

    @Before(Tx.class)
    public Record buyCollageByCouponId(String collageCouponId, String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("user.selectUserIntegral", loginUid);
        Record record = Db.findFirst(sqlPara);
        int integral = record.getInt("integral");
        sqlPara = Db.getSqlPara("shop.getCollageIntegralByCouponId", collageCouponId);
        Record coupon = Db.findFirst(sqlPara);
        if (null == coupon) {
            throw new BusinessException("此券已卖光");
        }
        String couponId = coupon.getStr("couponId");
        int couponIntegral = coupon.getInt("integral");
        if (integral < couponIntegral) {
            throw new BusinessException("乐币不够");
        }
        // 减少乐币
        sqlPara = Db.getSqlPara("user.reduceIntegral", couponIntegral, loginUid);
        int i = Db.update(sqlPara);
        if (i == 0) {
            throw new BusinessException("乐币不够");
        }
        //修改优惠券状态
        i = Db.update(Db.getSqlPara("shop.updateCollageByCouponId", collageCouponId));
        if (i == 0) {
            throw new BusinessException("优惠券不够");
        }

        String orderId = IdGen.uuid();
        String orderNumber = IdGen.getOrderIdByUUId();
        sqlPara = Db.getSqlPara("shop.getCollageByCouponId", collageCouponId);
        Record collage = Db.findFirst(sqlPara);
        Kv cond2 = Kv.by("id", IdGen.uuid());

        cond2.set("user_id", loginUid);
        cond2.set("state", "-1");
        cond2.set("orderNumber", orderNumber);
        cond2.set("product_id", collage.getStr("productId"));
        cond2.set("collage_id", collage.getStr("collageCouponId"));
        cond2.set("coupon_id", collage.getStr("couponId"));
        cond2.set("num", collage.getStr("num"));
        cond2.set("collage_order_id", orderId);
        Db.update(Db.getSqlPara("shop.saveCollageOrderDetail", cond2));

        Kv cond = Kv.by("user_id", loginUid);
        cond.set("state", "-1");
        cond.set("id", orderId);

        cond.set("integral", couponIntegral);
        cond.set("orderNumber", orderNumber);
        cond.set("product_id", collage.getStr("productId"));
        cond.set("collage_id", collage.getStr("collageCouponId"));
        cond.set("coupon_id", collage.getStr("couponId"));
        cond.set("num", collage.getStr("num"));
        cond.set("end_date", DateUtil.addDate(1));
        Db.update(Db.getSqlPara("shop.saveCollageOrder", cond));

        return new Record().set("collageOrderId", orderNumber);
    }

    @Before(Tx.class)
    public Record buyCollageByOrderId(String orderNumber, String loginUid) {
        SqlPara sqlPara = Db.getSqlPara("user.selectUserIntegral", loginUid);
        Record record = Db.findFirst(sqlPara);
        sqlPara = Db.getSqlPara("shop.getCollageOrderByCouponorderNumber", orderNumber);
        Record order = Db.findFirst(sqlPara);
        if (null == order) {
            throw new BusinessException("订单不存在");
        }
        if (order.getStr("state").equals("1")) {
            throw new BusinessException("已经拼团成功");
        }
        if (order.getStr("state").equals("0")) {
            throw new BusinessException("已经拼团失败");
        }
        int integral = record.getInt("integral");
        sqlPara = Db.getSqlPara("shop.getCollageIntegralByCouponId", order.getStr("collageId"));
        Record coupon = Db.findFirst(sqlPara);
        if (null == coupon) {
            throw new BusinessException("此券已卖光");
        }
        int num = order.getInt("num");
        sqlPara = Db.getSqlPara("shop.getCollageOrderDetailByCouponorderNumber", orderNumber);
        int orderDetailNum = Db.findFirst(sqlPara).getInt("num");
        if (num < orderDetailNum + 1) {
            throw new BusinessException("此拼团已满");
        }

        int couponIntegral = coupon.getInt("integral");
        if (integral < couponIntegral) {
            throw new BusinessException("乐币不够");
        }
        // 减少乐币
        sqlPara = Db.getSqlPara("user.reduceIntegral", couponIntegral, loginUid);
        int i = Db.update(sqlPara);
        if (i == 0) {
            throw new BusinessException("乐币不够");
        }
        //修改优惠券状态
        i = Db.update(Db.getSqlPara("shop.updateCollageByCouponId", order.getStr("collageId")));
        if (i == 0) {
            throw new BusinessException("优惠券不够");
        }

        String orderId = order.getStr("orderId");
        sqlPara = Db.getSqlPara("shop.getCollageByCouponId", order.getStr("collageId"));
        Record collage = Db.findFirst(sqlPara);
        Kv cond2 = Kv.by("id", IdGen.uuid());
        cond2.set("user_id", loginUid);
        cond2.set("state", "-1");
        cond2.set("orderNumber", orderNumber);
        cond2.set("product_id", collage.getStr("productId"));
        cond2.set("collage_id", collage.getStr("collageCouponId"));
        cond2.set("coupon_id", collage.getStr("couponId"));
        cond2.set("num", collage.getStr("num"));
        cond2.set("collage_order_id", orderId);
        Db.update(Db.getSqlPara("shop.saveCollageOrderDetail", cond2));
        if (num == orderDetailNum + 1) {
            collageOrderSuccess(orderNumber);
        }
        return new Record().set("collageOrderId", orderNumber);
    }


    private void collageOrderSuccess(String orderNumber) {
        Db.update(Db.getSqlPara("shop.collageOrderSuccess", orderNumber));
        Db.update(Db.getSqlPara("shop.collageOrderDetailSuccess", orderNumber));

        List<Record> rList = Db.find(Db.getSqlPara("shop.getUserCoupon", orderNumber));
        for (Record r : rList) {
            String userId = r.getStr("userId");
            String couponId = r.getStr("copuponId");
            Kv cond = Kv.create();
            cond.set("id", IdGen.uuid());
            cond.set("product_id", r.getStr("productId"));
            cond.set("coupon_id", r.getStr("copuponId"));
            cond.set("title", r.getStr("title"));
            cond.set("integral", r.getStr("integral"));
            cond.set("user_id", r.getStr("userId"));
            //增加优惠券
            Db.update(Db.getSqlPara("shop.updateFromCouponId",
                    IdGen.uuid(), r.getStr("productId"),
                    r.getStr("couponId"), r.getStr("title"), r.getStr("integral"), r.getStr("userId"))
            );
        }
    }


    @Before(Tx.class)
    public Record buyLuckyBurst(String burstCouponId, String loginUid) {

        SqlPara sqlBurstOrder= Db.getSqlPara("shop.getBurstOrderByIdAnd", burstCouponId,loginUid);
        Record recordBurstOrder = Db.findFirst(sqlBurstOrder);
        if(recordBurstOrder!=null){
            throw new BusinessException("已经购买此券，请重新刷新");
        }


        SqlPara sqlPara = Db.getSqlPara("user.selectUserIntegral", loginUid);
        Record record = Db.findFirst(sqlPara);
        if (record==null){
            throw new BusinessException("登录已过期，请重新登录");
        }
        int integral = record.getInt("integral");

        sqlPara = Db.getSqlPara("shop.getLuckyBurstById", burstCouponId);
        Record coupon = Db.findFirst(sqlPara);
        if (null == coupon) {
            throw new BusinessException("此券已卖光");
        }
        int couponIntegral = coupon.getInt("integral");
        if (integral < couponIntegral) {
            throw new BusinessException("乐币不够");
        }
        // 减少乐币
        sqlPara = Db.getSqlPara("user.reduceIntegral", couponIntegral, loginUid);
        Db.update(sqlPara);

        String orderNumber = IdGen.getOrderIdByUUId();
        Kv cond2 = Kv.by("id", IdGen.uuid());
        cond2.set("user_id", loginUid);
        cond2.set("state", "-1");
        cond2.set("orderNumber", orderNumber);
        cond2.set("product_id", coupon.getStr("productId"));
        cond2.set("burst_id", coupon.getStr("burstCouponId"));
        cond2.set("coupon_id", coupon.getStr("couponId"));
        cond2.set("num", 0);

        Db.update(Db.getSqlPara("shop.saveBurstOrderDetail", cond2));
        return new Record().set("orderNumber", orderNumber);
    }

    public Record luckyBurstDetail(String orderNumber, String loginUid) {



        Record r = Db.findFirst(Db.getSqlPara("shop.luckyBurstDetail", orderNumber));
        doImgPath(r);
        Record r4 = Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserRanking", r.getStr("burstCouponId"), r.getStr("userId")));
        doImgPath(r4);


        List<Record> list = Db.find(Db.getSqlPara("shop.getLuckyBurstUserRankingList", r.getStr("burstCouponId")));
        r.set("list", null);
        for (Record r3 : list
        ) {
            doImgPath(r3);
        }
        if (null != list && list.size() > 0) {
            for (int i = 0, s = list.size(); i < s; i++) {
                if (i > 0 && list.get(i).getInt("ranking").equals(list.get(i - 1).getInt("ranking"))) {
                    list.get(i).set("ranking", list.get(i - 1).getInt("ranking"));
                } else {
                    list.get(i).set("ranking", (i + 1));
                }

            }
            for (Record ra : list) {
                if (ra.getStr("id").equals(r4.getStr("id"))) {
                    r4.set("ranking", ra.getStr("ranking"));
                }
            }
            r.set("list", list);
        }
        r.set("u", r4);
        r.set("userCount", Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserCount", r.getStr("burstCouponId"))).getInt("userCount"));
        List<Record> advs = Db.find(Db.getSqlPara("shop.getLuckyBurstAdvs", r.getStr("burstCouponId")));
        for (Record r2 : advs
        ) {
            r2.set("img1", r2.getStr("imgSrc"));
            doImgPath(r2);
            r2.set("imgSrc", r2.getStr("img1"));
        }
        int currentHour, beginDate, endDate;

        currentHour = r.getInt("currentHour");
        beginDate = r.getInt("beginDate");
        endDate = r.getInt("endDate");

        r.set("sortDate", endDate - currentHour);
        if (currentHour >= beginDate && currentHour < endDate) {
            r.set("sortDate", 0);
        }
        r.set("advs", advs);
        r.set("totalNum", Db.findFirst(Db.getSqlPara("shop.getLuckyBurstTotalCount", r.getStr("burstCouponId"))).getInt("totalCount"));

        return r;

    }


    public Record luckyBurstDetailEx(String orderNumber, String loginUid,String typeId) {

        Record luckyBurstTypeRecord= Db.findFirst(Db.getSqlPara("shop.getLuckyBurstTypeById", typeId));

        if (luckyBurstTypeRecord==null) {
            return null;
        }


        int currentHour, beginDate, endDate;
        currentHour = luckyBurstTypeRecord.getInt("currentHour");
        beginDate = luckyBurstTypeRecord.getInt("beginDate");
        endDate = luckyBurstTypeRecord.getInt("endDate");

        Date beginTime = luckyBurstTypeRecord.getTime("beginDate1");
        Date endTime = luckyBurstTypeRecord.getTime("endDate1");

        Kv cond= new Kv().set("del","0").set("burst","and a.play_time   BETWEEN "+ " '"+beginTime + "' " + "and " + " '"+ endTime+"'");

//        List<Record> recordList =Db.find(Db.getSqlPara("shop.getLuckyBurstListEx1",cond));

//        Kv cond= new Kv().set("del","0").set("burst","and a.play_time   BETWEEN "+ " '"+beginTime + "' " + "and " + " '"+ endTime+"'");

        Record r =Db.findFirst(Db.getSqlPara("shop.luckyBurstDetailEx",orderNumber,cond));

//        Kv cond= new Kv().set("del","0").set("burst","and a.play_time   BETWEEN "+ " '"+beginTime + "' " + "and " + " '"+ endTime+"'");

//        List<Record> recordList =Db.find(Db.getSqlPara("shop.luckyBurstDetailEx",cond));
//
//        Record r = Db.findFirst(Db.getSqlPara("shop.luckyBurstDetail", orderNumber));
        doImgPath(r);


        Record r4 = Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserRanking", r.getStr("burstCouponId"), r.getStr("userId")));
        doImgPath(r4);


        List<Record> list = Db.find(Db.getSqlPara("shop.getLuckyBurstUserRankingList", r.getStr("burstCouponId")));
        r.set("list", null);
        for (Record r3 : list
                ) {
            doImgPath(r3);
        }
        if (null != list && list.size() > 0) {
            for (int i = 0, s = list.size(); i < s; i++) {
                if (i > 0 && list.get(i).getInt("ranking").equals(list.get(i - 1).getInt("ranking"))) {
                    list.get(i).set("ranking", list.get(i - 1).getInt("ranking"));
                } else {
                    list.get(i).set("ranking", (i + 1));
                }

            }
            for (Record ra : list) {
                if (ra.getStr("id").equals(r4.getStr("id"))) {
                    r4.set("ranking", ra.getStr("ranking"));
                }
            }
            r.set("list", list);
        }
        r.set("u", r4);
        r.set("userCount", Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserCount", r.getStr("burstCouponId"))).getInt("userCount"));
        List<Record> advs = Db.find(Db.getSqlPara("shop.getLuckyBurstAdvs", r.getStr("burstCouponId")));
        for (Record r2 : advs
                ) {
            r2.set("img1", r2.getStr("imgSrc"));
            doImgPath(r2);
            r2.set("imgSrc", r2.getStr("img1"));
        }
//        int currentHour, beginDate, endDate;

//        currentHour = r.getInt("currentHour");
//        beginDate = r.getInt("beginDate");
//        endDate = r.getInt("endDate");
        r.set("beginTime",beginTime);
        r.set("endTime",endTime);

        r.set("sortDate", 1);
        if (currentHour >= beginDate && currentHour < endDate) {
            //活动中
            r.set("sortDate", 0);
        }
        if (currentHour < beginDate) {
            //活动未开始
            r.set("sortDate", -1);
        }
//        r.set("sortDate", endDate - currentHour);
//        if (currentHour >= beginDate && currentHour < endDate) {
//            r.set("sortDate", 0);
//        }
        r.set("advs", advs);
        r.set("totalNum", Db.findFirst(Db.getSqlPara("shop.getLuckyBurstTotalCount", r.getStr("burstCouponId"))).getInt("totalCount"));

        return r;

    }
    private void setUserRanking(Record r2, String orderNumber) {
        Record r3 = Db.findFirst(Db.getSqlPara("shop.luckyBurstDetail", orderNumber));
        r2.set("u", Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserRanking", r3.getStr("burstCouponId"), r3.getStr("userId"))));
        r2.set("userCount", Db.findFirst(Db.getSqlPara("shop.getLuckyBurstUserCount", r3.getStr("burstCouponId"))).getInt("userCount"));
        r2.set("totalNum", Db.findFirst(Db.getSqlPara("shop.getLuckyBurstTotalCount", r3.getStr("burstCouponId"))).getInt("totalCount"));

    }

    @Before(Tx.class)
    public Record getLuckyBurstByAdv(String orderNumber, String loginUid, String luckyBurstAdvId) {
        Record record = Db.findFirst(Db.getSqlPara("shop.getUserLuckyBurstAdv", orderNumber, loginUid, luckyBurstAdvId));
        if (null == record || StrKit.isBlank(record.getStr("id"))) {
            record = Db.findFirst(Db.getSqlPara("shop.getLuckyBurstAdvById", luckyBurstAdvId));
            Db.update(Db.getSqlPara("shop.updateBurstOrderNum", record.getInt("num"), orderNumber));
            Kv cond2 = Kv.by("id", IdGen.uuid());
            cond2.set("user_id", loginUid);
            cond2.set("orderNumber", orderNumber);
            cond2.set("advId", luckyBurstAdvId);
            Db.update(Db.getSqlPara("shop.saveUserLuckyBurstAdv", cond2));
        }
        setUserRanking(record, orderNumber);
        return record;
    }

    @Before(Tx.class)
    public Record getLuckyBurstByFriend(String orderNumber, String loginUid) {
        Record record = Db.findFirst(Db.getSqlPara("shop.getUserLuckyBurstOrderDetail", orderNumber, loginUid));
        Record r = Db.findFirst(Db.getSqlPara("shop.luckyBurstDetail", orderNumber));
        r = Db.findFirst(Db.getSqlPara("user.isFriend", r.getStr("userId"), loginUid));
        if (r != null) {
            if ((StrKit.notBlank(r.getStr("id")))
                    && (null == record || StrKit.isBlank(record.getStr("id")))) {
                int result = Db.update(Db.getSqlPara("shop.updateBurstOrderNum", 1, orderNumber));
                Kv cond2 = Kv.by("id", IdGen.uuid());
                cond2.set("user_id", loginUid);
                cond2.set("orderNumber", orderNumber);
                Db.update(Db.getSqlPara("shop.saveUserLuckyBurstDetail", cond2));
                record = new Record();
            }
        } else {
            throw new BusinessException("非好友不能助力");
        }
        setUserRanking(record, orderNumber);
        return record;
    }


    @Before(Tx.class)
    public Record getLuckyBurstByFriendEx(String orderNumber) {

        Record record=new Record();
        int result = Db.update(Db.getSqlPara("shop.updateBurstOrderNum", 1, orderNumber));
        setUserRanking(record, orderNumber);
        return record;
    }

    public int getTotalFeeByOrderId(String type, String ordersId) {
        int f = 1;
        if (type.equals("1")) {
            String orders[] = ordersId.split(",");
            f = Db.findFirst(Db.getSqlPara("mediaShop.getPayIntegral", Kv.by("ids", orders))).getInt("payIntegral");
        } else if (type.equals("2")) {
            f = Db.findFirst(Db.getSqlPara("mediaShop.getUserRechargeOrder", ordersId)).getInt("integral");
        } else if (type.equals("3")) {
            f = (int) (Db.findFirst(Db.getSqlPara("mediaShop.getShopCouponOrder", ordersId)).getDouble("integral") * 100);
        } else if (type.equals("4")) {
            f = Db.findFirst(Db.getSqlPara("mediaShop.getGameShopOrder", ordersId)).getInt("rent") * 100;
        } else if (type.equals("5")) {
            f = Db.findFirst(Db.getSqlPara("mediaShop.getGameRechargeOrder", ordersId)).getInt("integral");
        }
        return f;
    }

    @Before(Tx.class)
    public void updateState(String type, String transaction_id, String ordersId) {
        //type 1乐购订单 2app充值 3后台充值 4游戏订单 5游戏充值
        if (type.equals("1")) {
            String orders[] = ordersId.split(",");
            for (String o : orders) {

                Db.update(Db.getSqlPara("mediaShop.updateOrderPayInfoTransactionid", o, transaction_id));
                Db.update(Db.getSqlPara("mediaShop.updateOrderDetailPayInfo", o));

            }
        } else if (type.equals("2")) {
            Record r = Db.findFirst(Db.getSqlPara("mediaShop.getUserRechargeOrder", ordersId));
            int f = r.getInt("integral");
            if (f >= 10000) {
                f = Math.floorDiv(f, 10000) * 200;
            }
            Db.update(Db.getSqlPara("mediaShop.updateUserRechargeOrder", ordersId, transaction_id));
            UserService userService = Enhancer.enhance(UserService.class);
            userService.addIntegral(r.get("userId"), f, "用户充值");
        } else if (type.equals("3")) {
            Record r = Db.findFirst(Db.getSqlPara("mediaShop.getShopCouponOrder", ordersId));
            int f = r.getInt("integral") * 100;
            Db.update(Db.getSqlPara("mediaShop.updateShopCouponOrder", ordersId, transaction_id));
            UserService userService = Enhancer.enhance(UserService.class);
            userService.addIntegral(r.get("userId"), f, "用户充值");
        } else if (type.equals("4")) {
            Record r = Db.findFirst(Db.getSqlPara("game.getShopOrderByorderNumber", ordersId));
            Db.update(Db.getSqlPara("game.updateShopOrder", ordersId, transaction_id));
            Db.update(Db.getSqlPara("game.updateShopByState", r.getStr("userId"), r.get("shopId"), DateUtil.addYear(1)));
            Db.update(Db.getSqlPara("game.updateShelves", r.getStr("userId"), r.get("shopId")));
        } else if (type.equals("5")) {
            Record r = Db.findFirst(Db.getSqlPara("mediaShop.getGameRechargeOrder", ordersId));
            int f = r.getInt("integral");
            if (f >= 10000) {
                f = Math.floorDiv(f, 10000) * 200;
            }
            Db.update(Db.getSqlPara("mediaShop.updateGameRechargeOrder", ordersId, transaction_id));
            UserService userService = Enhancer.enhance(UserService.class);
            userService.addIntegral(r.get("userId"), f, "游戏充值");
        }
    }

    @Before(Tx.class)
    public void insetAfter(Map<String, String> decMap) {
        //修改退款状态为退款完成
        Kv cond = Kv.by("id", IdGen.uuid()).set("transaction_id", decMap.get("transaction_id")).set("out_trade_no", decMap.get("out_trade_no")).set("out_refund_no", decMap.get("out_refund_no"))
                .set("refund_id", decMap.get("refund_id")).set("refund_fee", Integer.valueOf(decMap.get("settlement_refund_fee"))).set("total_fee", Integer.valueOf(decMap.get("total_fee")));
        //修改退款状态
        Db.update(Db.getSqlPara("sell.updateOrderAfter", cond));
        //添加退款日志
        Db.update(Db.getSqlPara("sell.insertOrderAfterLog", cond));
        //查询支付乐币
        Record record = Db.findFirst(Db.getSqlPara("sell.getOrderByOrderNumber", cond));
        //退还用户乐币
        Db.update(Db.getSqlPara("sell.updateUserInById", record.getStr("payIntegral"), record.getStr("orderNumber")));
    }

}
